package minderupt.selenate.dependency;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;

import java.io.InputStream;
import java.net.URL;

/**
 *
 */
public class DependencyManager {

    private ApplicationContext appContext = null;

    private static DependencyManager dependencyManager;
    private static Log LOGGER = LogFactory.getLog(DependencyManager.class);


    private DependencyManager() {
        init();

    }

    public static DependencyManager getDependencyManager() {

        if (dependencyManager == null) {
            dependencyManager = new DependencyManager();
        }

        return (dependencyManager);
    }


    public Object getDependencyByName(String name) {

        return (this.appContext.getBean(name));


    }

    public Resource getResource(String location) {

        return (this.appContext.getResource(location));


    }


    public InputStream getResourceAsInputStream(String path) {

        InputStream is = this.getClass().getResourceAsStream(path);
        return(is);

    }

    public URL getResourceAsURL(String path) {
        URL url = this.getClass().getResource(path);
        return(url);
    }

    private void init() {

        if (LOGGER.isDebugEnabled()) LOGGER.debug("Initializing DI!");
        this.appContext = new ClassPathXmlApplicationContext("classpath:/spring-config/*.xml");
        if(LOGGER.isDebugEnabled()) LOGGER.debug("Init COMPLETE!");

    }


}
